#!/usr/bin/python
# -*- coding: utf-8 -*-

import pandas as pd
import sys


# 获取参数工具类
class ParameterUtil:

    @staticmethod
    def get_data(code):
        return pd.read_csv('./web/stockDetail/' + code + '/data.csv')

    # 获取趋势(当天收盘价-n天前的收盘价)/n天前收盘价
    @staticmethod
    def get_trend(code, days):
        data = ParameterUtil.get_data(code)
        trend_arr = []
        for i in data.index:
            close_now = data.loc[i, '收盘']
            before_i = (i - days) if (i - days) >= 0 else 0
            close_before = data.loc[before_i, '收盘']
            trend_item = (close_now - close_before) / close_before
            trend_arr.append(trend_item)
        trend_series = pd.Series(trend_arr, name='trend')
        print(trend_series)
        return trend_series

    @staticmethod
    def get_trend_sum(code, days):
        data = ParameterUtil.get_data(code)
        trend_arr = []
        for i in data.index:
            close_now = data.loc[i, '收盘']
            before_i = (i - days) if (i - days) >= 0 else 0
            close_before = data.loc[before_i, '收盘']
            trend_item = (close_now - close_before) / close_before
            trend_arr.append(trend_item)
        trend_series = pd.Series(trend_arr, name='trend')
        trend_sum_series = trend_series.rolling(days).sum()
        trend_sum_series.bfill(inplace=True)
        trend_sum_series.name = 'trend_sum'
        print(trend_sum_series)
        return trend_sum_series

    @staticmethod
    def get_MA(code, days):
        data = ParameterUtil.get_data(code)
        close_data = data['收盘']
        MA = close_data.rolling(days).mean().round(2)
        MA.bfill(inplace=True)
        print(MA)
        return MA

    # 涨幅(0.01 = 1%)
    @staticmethod
    def get_increase(code):
        data = ParameterUtil.get_data(code)
        increase = (data.loc[:, '收盘'] - data.loc[:, '开盘']) / data.loc[:, '开盘']
        increase.name = 'increase'
        print(increase)
        return increase

    # 前days天是否全为阳线
    @staticmethod
    def get_is_all_positive(code, days):
        increase = ParameterUtil.get_increase(code)
        is_all_positive_arr = []
        for increase_index in increase.index:
            # 循环days天
            flag = True
            for day in range(days):
                if increase_index - day - 1 >= 0 and increase[increase_index - day - 1] < 0:
                    flag = False
                    break
            is_all_positive_arr.append(flag)
        is_all_positive_series = pd.Series(is_all_positive_arr, name='is_positive')
        print(is_all_positive_series)
        return is_all_positive_series

    # days天前是否为阳
    @staticmethod
    def get_is_positive(code, days):
        increase = ParameterUtil.get_increase(code)
        is_positive_arr = []
        for increase_index in increase.index:
            flag = False
            if increase_index - days > 0 and increase[increase_index - days] > 0:
                flag = True
            is_positive_arr.append(flag)
        is_positive_series = pd.Series(is_positive_arr, name='is_positive')
        print(is_positive_series)
        return is_positive_series

    def get_max_of_days(code, days):
        data = ParameterUtil.get_data(code)
        max_after_buy = -sys.maxsize
        close = data.loc['收盘']
        for i in data.index:
            print()





# Parameter.get_trend('600519', 20)

# Parameter.get_MA('600519', 5)


# Parameter.get_trend('600519', 2)
# Parameter.get_trend_sum('600519', 2)


# ParameterUtil.get_increase('600519')
#
# ParameterUtil.get_is_positive('600519', 0)
